#ifndef _RT_DATABASE_H__
#define _RT_DATABASE_H__

#include "RtDefines.h"
#include "RtDebug.h"

#ifdef __ORACLE__
#include "dboci.h"
#else
#include "dbmysql.h"
#endif

/*--------------------Return value definition-------------------------*/
#define RT_DB_SUCCESS				0
#define RT_DB_FAILED				0x1000
#define RT_DB_IS_DOWN				(RT_DB_FAILED + 1)
#define RT_DB_WRONG_SERVER			(RT_DB_FAILED + 2)
#define RT_DB_WRONG_DB				(RT_DB_FAILED + 3)
#define RT_DB_WRONG_USER_OR_PASSWD	(RT_DB_FAILED + 4)
#define RT_DB_INVALID_SQL_STMT		(RT_DB_FAILED + 5)
#define RT_DB_SQL_TOO_LONG			(RT_DB_FAILED + 6)
#define RT_DB_NO_LISTENER			(RT_DB_FAILED + 7)
#define RT_DB_OTHER_ERR				(RT_DB_FAILED + 8)
#define RT_DB_OCINUMBER_IS_INTEGER	(RT_DB_FAILED + 9)
/*--------------------------------------------------------------------*/

/*--------------------------------------------------------------*/
/*                                                              */
/*      class CDBOciAdaptor                                     */
/*                                                              */
/*--------------------------------------------------------------*/
class RT_API_EXPORT CDBOciAdaptor
{
public :
	CDBOciAdaptor(LPCSTR szDBSvr,
		LPCSTR szDBName,
		LPCSTR szUserName,
		LPCSTR szPasswd);
	virtual ~CDBOciAdaptor();
	
public :
	RtResult OpenDatabase();
	RtResult CloseDatabase();
	
	/* Default it execute SQL statement */
#ifdef __ORACLE__
	RtResult PutDataInDB(LPCSTR szSQLStmt, short sType = IS_STATEMENT);
	
	RtResult GetDataFromDB(LPCSTR szSQLStmt, short sType = IS_STATEMENT);	
#else
	RtResult PutDataInDB(LPCSTR szSQLStmt, short sType = 0);
	
	RtResult GetDataFromDB(LPCSTR szSQLStmt, short sType = 0);	
	RtResult InsertAutoIncRow(LPCSTR szSQLStml, LONGLONG& ulAutoIncNumber);
#endif
	CRecordset_* GetRecordSet() { return m_pRecordset; }
	
	BOOL IsConnected() { return m_bConnected; };
	
	RtResult TransErrRetValue(int nRetCode, const char* strErr = NULL);

#ifdef __ORACLE__
	RtResult PrepareStmt(LPCSTR pszSQLStmt);
	RtResult BindStmtInParam(LPCSTR pszParamName, DWORD &dwContent);
	RtResult BindStmtInParam(LPCSTR pszParamName, LPBYTE pszContent);
	RtResult ExecutePreparedSQLStmt();
	RtResult BindStmtOutParam(LPCSTR pszParamName, DWORD &dwContent);
	RtResult BindStmtOutParam(LPCSTR pszParamName, LPBYTE pszContent);
public:
	RtResult GetIntFromOCINumber(OCINumber* pnumber, DWORD &dwVal);
	RtResult GetIntFromOCINumber(OCINumber* pnumber, WORD &wVal);
	RtResult GetUIntFromOCINumber(OCINumber* pnumber, DWORD &dwVal);
	RtResult GetRealFromOCINumber(OCINumber* pnumber, double &fVal);
	RtResult GetGMTTimeFromOCIDate(OCIDate*  ptime, gmt_time*  tm);
#endif
	int GetErrCode(); // Get Current ORA SQLERRM

public:
	RtResult HandleException(CDBException_* e);

protected:
	CDatabase_	m_pDatabase;
	CRecordset_*	m_pRecordset;
	
	LPSTR		m_szDBSvr[_MAX_DBLENGTH];
	LPSTR		m_szDBName[_MAX_DBLENGTH];
	LPSTR		m_szUserName[_MAX_DBLENGTH];
	LPSTR		m_szPasswd[_MAX_DBLENGTH];
	
	BOOL		m_bConnected;

	int			m_nErrCode;
};

#endif //_RT_DATABASE_H__
